<template>
	<view>
		<view class="title-line">

		</view>
		<view class="content-box">
			<view class="identity-box">
				<view class="top">
					便于审核通过，请务必提供清晰证件,保持人证为同一人.
				</view>
				<view class="bottom">
					<view class="left">
						<view class="top">
							身份证正面
						</view>
						<image @tap="selectImage(1)" class="bottom"
							:src="formData.idcard_just?formData.idcard_just:$img('/static/2021-08-30/sfzz.png')"
							mode="widthFix"></image>
					</view>
					<view class="left">
						<view class="top">
							身份证反面
						</view>
						<image class="bottom" @tap="selectImage(2)"
							:src="formData.idcard_opposite?formData.idcard_opposite:$img('/static/2021-08-30/sfzf.png')"
							mode="widthFix"></image>
					</view>
				</view>
			</view>
			<view class="msg-box">
				<view class="msg">
					<view class="top">
						姓名
					</view>
					<view class="bottom">
						<input class="left" v-model="formData.realname" placeholder="请输入真实姓名" type="text" />
						<image class="right" :src="$img('/static/2021-08-30/cha2.png')" @tap="null_realname" mode="">
						</image>
					</view>
				</view>
				<view class="msg">
					<view class="top">
						身份证号
					</view>
					<view class="bottom">
						<input class="left" maxlength="18" v-model="formData.id_card" placeholder="请输入身份证号码"
							type="text" />
						<image class="right" :src="$img('/static/2021-08-30/cha2.png')" @tap="null_idcard_just" mode="">
						</image>
					</view>
				</view>
			</view>
			<view class="text-sh">
				上传身份证只是为了审核，绝对不会泄漏你的任何隐私
			</view>
		</view>
		<view class="submit" @tap="submit()">
			确认提交
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formData: {
					idcard_just: "", //身份证正面
					idcard_opposite: "", //身份证反面
					id_card: "", //身份证号
					realname: "", //真实姓名
				}
			}
		},
		onLoad(){
			this.get_data();
		},
		methods: {
			// 获取实名认证信息
			get_data(){
				let data = {
					
				}
				this.$utils.request('/api/useridcard/index',data,res=>{
					this.formData.idcard_just = res.data.idcard_just;
					this.formData.idcard_opposite = res.data.idcard_opposite;
					this.formData.id_card = res.data.id_card;
					this.formData.realname = res.data.realname;
				})
			},
			null_realname() {
				this.formData.realname = "";
			},
			null_idcard_just() {
				this.formData.id_card = "";
			},
			// 上传身份证
			selectImage(type) {
				// console.log(this.$img('/api/upload'));
				wx.chooseImage({
					count: 1,
					success: (res) => {
						const tempFilePaths = res.tempFilePaths[0];
						wx.uploadFile({
							url: this.$img('/api/upload'),
							filePath: tempFilePaths,
							name: 'file',
							header: {
								"Content-Type": "application/json",
								"Authorization": 'Bearer ' + wx.getStorageSync('token')
							},

							success: (res) => {
								if (res.statusCode == 200) {
									// console.log(res);
									let ress = JSON.parse(res.data);

									if (ress.code == 200) {
										if (type == 1) {
											this.formData.idcard_just = ress.data;
										} else if (type == 2) {
											this.formData.idcard_opposite = ress.data;
										}
									}
								}
							}

						});
					}

				});
			},
			// 提交实名认证信息
			submit() {
				for (let i in this.formData) {
					if (this.formData[i] == "") {
						uni.showToast({
							title: "请完善信息",
							icon: 'none'
						})
						return
					}
				}

				// 提交
				this.$utils.postrequest("/api/useridcard/edit", this.formData, res => {
					if (res) {
						if (res.code === 200) {
							uni.showToast({
								icon: 'success',
								title: "提交成功"
							})
							setTimeout(() => {
								uni.switchTab({
									url: "/pages/my/index"
								})
							}, 2000)
						}else{
							uni.showToast({
								title:res.message,
								icon:"none"
							})
						}
					}
				})

			}
		}
	}
</script>

<style lang="scss" scoped>
	@import "./advanced-certification.scss";
</style>
